// 引入express
const express = require("express");
// 引入数据库
const sql = require("../pool.js");
// 创建路由器对象
const route = express.Router();
// 添加用户注册路由
route.post("/register", (req, res) => {
  sql.query("insert into zm_user set ?", [req.body], (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);
  
  if (r.affectedRows) {
    res.send("注册成功!");
  } else {
    res.send("注册失败!");
  }});
});
// 添加查询用户信息路由
route.get("/userInfo", (req, res) => {
  sql.query("select * from zm_user", (err, r) => {
    if (err) {
      throw err;
    }
    console.log(r);

    if (r.length) {
      res.send({ code: 200, msg: "查询成功", data: r });
    } else {
      res.send({ code: 400, msg: "查询失败" });
    }
  });
});
// 用户更改路由
route.put("/userUpdate", (req, res) => {
  sql.query(
    "update zm_user set ? where uid=?",
    [req.body, req.body.uid],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);

      if (r.affectedRows) {
        res.send("更改成功!");
      } else {
        res.send("更改失败!");
      }

      console.log(req.body);
    }
  );
});
// 用户删除路由
route.get("/userDelete/:uname", (req, res) => {
  sql.query(
    "delete from zm_user where uname = ?",
    [req.params.uname],
    (err, r) => {
      if (err) {
        throw err;
      }
      console.log(r);

      if (r.affectedRows) {
        res.send("删除成功!");
      } else {
        res.send("删除成功!");
      }
    }
  );
});
module.exports = route;
